/*
 * junzhinuo all rights received.
 */
package com.junzhinuo.cqpzxx.adapter.wordpress;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.photobucket.jackson.dataformat.php.PhpFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Didy
 */
@Repository
public class WordPressAdapter {

    @PersistenceContext
    EntityManager em;

    public WpPosts getPost(Long id) {
        return em.createNamedQuery("WpPosts.findById", WpPosts.class)
                .setParameter("id", id)
                .getSingleResult();
    }

    public Collection<WpPosts> getPostsByCategory(Long categoryId, int offset, int limit) throws IOException {
        String sql = "select * from wp_posts p \n"
                + "	left join wp_term_relationships tr on tr.object_id=p.id \n"
                + "	left join wp_term_taxonomy tt on tt.term_taxonomy_id=tr.term_taxonomy_id \n"
                + "	left join wp_terms t on t.term_id=tt.term_id\n"
                + "	where tt.taxonomy=:taxonomy and tt.term_id=:termId"
                + "     and p.post_type='post' and p.post_status='publish' order by p.post_date desc";

        List resultList = em.createNativeQuery(sql, WpPosts.class)
                .setParameter("taxonomy", "category")
                .setParameter("termId", categoryId)
                .setFirstResult(offset)
                .setMaxResults(limit)
                .getResultList();

        Map<String, String> options = getOptions();
        String phpSource = options.get("sticky_posts");
        System.out.println("phpSource:" + phpSource);
        ObjectMapper mapper = new ObjectMapper(new PhpFactory());
        Map<Object, Long> readValue = mapper.readValue(phpSource, Map.class);
        List stickyPosts = new ArrayList();
        for (Long stickyId : readValue.values()) {
            WpPosts post = getPost(stickyId);
            if (resultList.contains(post)) {
                stickyPosts.add(post);
                resultList.remove(post);
            }
        }
        System.out.println("readValue:" + stickyPosts);

        stickyPosts.addAll(resultList);

        return stickyPosts;

    }

    public WpPosts getPage(String name) {
        return (WpPosts) em.createNativeQuery("select * from wp_posts p where p.post_type='page' and p.post_status='publish' and p.post_name=:name", WpPosts.class)
                .setParameter("name", name)
                .getSingleResult();
    }

    public Collection<WpPosts> getSubpages(Long id) {
        return em.createNativeQuery("select * from wp_posts p where p.post_type='page' and p.post_status='publish' and p.post_parent=:id order by p.menu_order", WpPosts.class)
                .setParameter("id", id)
                .getResultList();
    }

    public String getPostCover(Long id) {
        try {
            return (String) em.createNativeQuery("select p.guid from wp_posts p where p.post_mime_type='image/jpeg' and p.post_type='attachment' and p.post_parent=:id order by p.ID desc")
                    .setParameter("id", id)
                    .setMaxResults(1)
                    .getSingleResult();
        } catch (NoResultException ex) {
            return null;
        }
    }

    public Map<String, String> getOptions() {
        String sql = "SELECT * FROM bcc_homepage.wp_options;";
        List<WpOptions> resultList = em.createNativeQuery(sql, WpOptions.class).getResultList();
        Map<String, String> options = new HashMap();
        for (WpOptions option : resultList) {
            options.put(option.getOptionName(), option.getOptionValue());
        }

        return options;
    }

}
